package com.anxin.controller;

import com.anxin.common.PageResult;
import com.anxin.common.Result;
import com.anxin.entity.MedicalRecord;
import com.anxin.service.MedicalRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 病历记录控制器
 */
@Tag(name = "病历管理")
@RestController
@RequestMapping("/api/medical-record")
public class MedicalRecordController extends BaseController {

    @Autowired
    private MedicalRecordService medicalRecordService;

    /**
     * 创建病历记录
     */
    @Operation(summary = "创建病历记录")
    @PostMapping
    public Result<Boolean> create(@RequestBody MedicalRecord medicalRecord) {
        return Result.success(medicalRecordService.save(medicalRecord));
    }

    /**
     * 更新病历记录
     */
    @Operation(summary = "更新病历记录")
    @PutMapping
    public Result<Boolean> update(@RequestBody MedicalRecord medicalRecord) {
        return Result.success(medicalRecordService.updateById(medicalRecord));
    }

    /**
     * 删除病历记录
     */
    @Operation(summary = "删除病历记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable String id) {
        return Result.success(medicalRecordService.removeById(id));
    }

    /**
     * 获取病历记录详情
     */
    @Operation(summary = "获取病历记录详情")
    @GetMapping("/{id}")
    public Result<MedicalRecord> getById(@PathVariable String id) {
        return Result.success(medicalRecordService.getById(id));
    }

    /**
     * 分页查询病历记录
     */
    @Operation(summary = "分页查询病历记录")
    @GetMapping("/page")
    public Result<PageResult<MedicalRecord>> page(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "患者ID") @RequestParam(required = false) String patientId,
            @Parameter(description = "医生ID") @RequestParam(required = false) String doctorId,
            @Parameter(description = "科室ID") @RequestParam(required = false) String departmentId
    ) {
        Page<MedicalRecord> page = medicalRecordService.page(pageNum, pageSize, patientId, doctorId, departmentId);
        return Result.page(page);
    }

    /**
     * 获取患者的病历记录列表
     */
    @Operation(summary = "获取患者的病历记录列表")
    @GetMapping("/patient/{patientId}")
    public Result<List<MedicalRecord>> getByPatientId(@PathVariable String patientId) {
        return Result.success(medicalRecordService.getByPatientId(patientId));
    }

    /**
     * 获取医生的病历记录列表
     */
    @Operation(summary = "获取医生的病历记录列表")
    @GetMapping("/doctor/{doctorId}")
    public Result<List<MedicalRecord>> getByDoctorId(@PathVariable String doctorId) {
        return Result.success(medicalRecordService.getByDoctorId(doctorId));
    }

    /**
     * 获取科室的病历记录列表
     */
    @Operation(summary = "获取科室的病历记录列表")
    @GetMapping("/department/{departmentId}")
    public Result<List<MedicalRecord>> getByDepartmentId(@PathVariable String departmentId) {
        return Result.success(medicalRecordService.getByDepartmentId(departmentId));
    }
} 